/**
 * ajax设置。用于ajax预处理和处理完成后事件。场景：验证用户登录等
 */
$.ajaxSetup({
    beforeSend:function(XMLHttpRequest){
    	if($('#_loading').length == 0) {
    		$('body').append(' <div id="_loading" style="width:66px;height:66px;position:absolute;top:50%;left:50%;"> <img  alt="加载中..." src="../static/img/loading.gif"/> </div>');
    	}
    }, 
    afterSend:function(xhr,data) {
    }, 
    complete:function(xhr,data) {
    	var Authorized = xhr.getResponseHeader('Authorized');
    	if(Authorized && Authorized == "false"){
    		alert("您未登录，或者已经过期，请从新登录！");
    		//xhr.abort();
    		window.location.href=(path || '' )+"/user/login?referer="+window.location.pathname.replace(path+"/", "/");
    	}
    	$('#_loading').remove();
    }
});

//对空字符串进行转行
function checkUndefined(text) {
    var result = "";
    if(text == null || text == "undefined" || text == "null" || text =="") {
        result = "--";
    } else {
        result = text;
    }
    return result;
}

//时间格式转换
function dateForm(time, formatter) {
	Date.prototype.format = function(format) {
		var o = {
			"M+" : this.getMonth() + 1,
			"d+" : this.getDate(),
			"h+" : this.getHours(),
			"m+" : this.getMinutes(),
			"s+" : this.getSeconds(),
			"q+" : Math.floor((this.getMonth() + 3) / 3),
			"S" : this.getMilliseconds()
		};
		if (/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		}
		for ( var k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1,RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
			}
		}
		return format;
	};
	var date = new Date(time);
	formatter = formatter || "yyyy-MM-dd hh:mm:ss";
	return date.format(formatter);
}

function getTime(endTime,startTime){
	var date3 = endTime - startTime;  //时间差的毫秒数
	//计算出相差天数
	var days=Math.floor(date3/(24*3600*1000));
	//计算出小时数
	var leave1=date3%(24*3600*1000);    //计算天数后剩余的毫秒数
	var hours=Math.floor(leave1/(3600*1000));
	//计算相差分钟数
	var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
	var minutes=Math.floor(leave2/(60*1000));
	//计算相差秒数
	var leave3=leave2%(60*1000);      //计算分钟数后剩余的毫秒数
	var seconds=Math.round(leave3/1000);
	var time = seconds+" 秒";
	if(minutes != 0){
		time = minutes+" 分钟"+time;
	}
	if(hours != 0){
		time = hours+" 小时 "+ time;
	}
	if(days != 0){
		time = days+" 天 "+ time;
	}
	return time;
}

//设置分页
function setupPageChoose(curPage, totalPage,pageChooseDiv,curPageDiv,Func) {
	if (totalPage != 0) {
		$('#'+pageChooseDiv).bootstrapPaginator({
			bootstrapMajorVersion : 3,
			currentPage : curPage,
			totalPages : totalPage,
			numberOfPages : 10,
			size : "small",
			alignment : "center",
			itemTexts : function(type, page, current) {
				switch (type) {
				case "first":
					return "首页";
				case "prev":
					return "上一页";
				case "next":
					return "下一页";
				case "last":
					return "尾页";
				case "page":
					return page;
				}
			},
			onPageClicked : function(e, originalEvent, type, page) {
				$('#'+curPageDiv).val(page);
				Func();
			}
		});
	}
}